package my.jwds.p53;

public class MaxSubArray {
    public static void main(String[] args) {
        MaxSubArray o = new MaxSubArray();
        int [] nums = {-2,1,-3,4,-1,2,1,-5,4};
        System.out.println(o.maxSubArray(nums));
    }

    public int maxSubArray(int[] nums) {
        int max = Integer.MIN_VALUE;
        int prev = 0;
        for (int i = 0; i < nums.length; i++) {
            prev+= nums[i];
            if (prev <nums[i]){
                prev = nums[i];
            }
            if (max < prev){
                max = prev;
            }
        }
        return max;
    }
}
